package com.example.project.dao.impl;

import com.example.project.dao.ILeavesDao;

import com.example.project.entity.Leaves;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class LeavesDaoImpl implements ILeavesDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //实现请假添加方法
    @Override
    public int add(Leaves leaves) {
        return jdbcTemplate.update("insert into leaves(LeaveId,StartDate,EndDate,Reason,Ratifiers,EmployeeId)"+ "values (?,?,?,?,?,?)",
                leaves.getLeaveId(),
                leaves.getStartDate(),
                leaves.getEndDate(),
                leaves.getReason(),
                leaves.getRatifiers(),
                leaves.getEmployeeId()
                );
    }

    //实现查询所有请假数据的方法
    @Override
    public List<Leaves> getAllLeaves(Leaves leaves) {
        return jdbcTemplate.query("select LeaveId,StartDate,EndDate,Reason,Ratifiers,EmployeeId from leaves",new Object[]{},
                new BeanPropertyRowMapper<>(Leaves.class));
    }

    //删除请假数据
    @Override
    public int deleteLeaves(Leaves leaves) {
        return jdbcTemplate.update("delete from leaves where LeaveId=?",
                leaves.getLeaveId());
    }

    //实现请假信息的更新
    @Override
    public int updateLeaves(Leaves leaves) {
        return jdbcTemplate.update("update leaves set"+" StartDate=?,EndDate=?,Reason=?,Ratifiers=?,EmployeeId=? where LeaveId=?",
                leaves.getStartDate(),
                leaves.getEndDate(),
                leaves.getReason(),
                leaves.getRatifiers(),
                leaves.getEmployeeId(),
                leaves.getLeaveId());
    }

    //实现单挑查询请假数据方法
    @Override
    public Leaves getLeaves(Leaves leaves) {
        List<Leaves> result =jdbcTemplate.query("select"+"`LeaveId`,`StartDate`,`EndDate`,`Reason`,`Ratifiers`,`EmployeeId`"+"from leaves where LeaveId='"+leaves.getLeaveId()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Leaves.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
